﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace MotionCS.Alarm
{
    public enum ErrorCodeE
    {
        /// <summary>
        /// 急停开关
        /// </summary>
        EmgStop = 0x01,
        /// <summary>
        /// 安全门
        /// </summary>
        SafeDoor = 0x02,
        /// <summary>
        /// 气压
        /// </summary>
        AirPressure = 0x03,
        /// <summary>
        /// 相机X轴
        /// </summary>
        CamAxisX = 0x100,
        /// <summary>
        /// 相机Y轴
        /// </summary>
        CamAxisY = 0x110,
        /// <summary>
        /// 相机在线
        /// </summary>
        CamOnline = 0x120,
        /// <summary>
        /// 取像超时
        /// </summary>
        CamGrabTimeout = 0x130,
        /// <summary>
        /// 传送超时
        /// </summary>
        ConveyorTimeout = 0x120,
        /// <summary>
        /// 调宽轴
        /// </summary>
        ConveyorAxisW = 0x130,
    }

    public class AlarmInfo
    {
        /// <summary>
        /// 报警错误码
        /// </summary>
        public int ErrorCode { get; set; }
        /// <summary>
        /// 报警描述
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 报警状态
        /// </summary>
        public bool IsAlarm { get; set; }
    }

    public class AlarmDevice
    {
        /// <summary>
        /// 单例锁对象
        /// </summary>
        private static object objLock = new object();
        /// <summary>
        /// 单例的实例
        /// </summary>
        private static AlarmDevice instance;
        public static AlarmDevice Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (objLock)
                    {
                        if (instance == null)
                        {
                            instance = new AlarmDevice();
                        }
                    }
                }

                return instance;
            }
        }
        /// <summary>
        /// 报警
        /// </summary>
        private List<AlarmInfo> alarmItems = new List<AlarmInfo>();
        public List<AlarmInfo> AlarmItems
        {
            get => alarmItems;
            set
            {
                alarmItems = value;
            }
        }
        /// <summary>
        /// 报警通知回调
        /// </summary>
        private Action<AlarmInfo> alarmCallback;

        /// <summary>
        /// 注册报警回调
        /// </summary>
        /// <param name="callback"></param>
        public void RegisterAlarmCallback(Action<AlarmInfo> callback)
        {
            alarmCallback = callback;
        }
        /// <summary>
        /// 报警上报
        /// </summary>
        /// <param name="alarmInfo"></param>
        public void AlarmReport(AlarmInfo alarmInfo)
        {
            alarmCallback?.Invoke(alarmInfo);
        }
        /// <summary>
        /// 报警添加
        /// </summary>
        /// <param name="alarm"></param>
        public void AlarmItemsAdd(AlarmInfo alarm)
        {
            lock (objLock)
            {
                alarmItems.Add(alarm);
            }
        }
        /// <summary>
        /// 报警删除
        /// </summary>
        /// <param name="alarmID"></param>
        public void AlarmItemsRemove(int alarmID)
        {
            lock (objLock)
            {
                alarmItems.RemoveAll(itm => itm.ErrorCode == alarmID);
            }
        }
        /// <summary>
        /// 报警清除
        /// </summary>
        public void AlarmItemsClear()
        {
            lock (objLock)
            {
                alarmItems.Clear();
            }
        }
        /// <summary>
        /// 是否报警
        /// </summary>
        /// <returns></returns>
        public bool IsAlarm()
        {
            return alarmItems.Count > 0;
        }
    }
}
